package com.help.controller;

import com.help.common.api.CommonResult;
import com.help.domain.Feedback;
import com.help.domain.FeedbackPojo;
import com.help.service.impl.FeedbackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;

@Api(tags = "反馈")
@RequestMapping("feedback")
@RestController
@Slf4j
public class FeedbackController {

    @Autowired
    public FeedbackService feedbackService;

    private String CACHE_NAME="data";

    private HashMap<String,Object> cache=new HashMap<>();


    @PostConstruct
    public void setCache(){
        cache.put(CACHE_NAME,getList());
    }

    @ApiOperation("添加反馈")
    @PostMapping("/add")
    public CommonResult add(@RequestBody FeedbackPojo feedbackPojo){
        Feedback feedback = new Feedback(feedbackPojo);
        boolean save = feedbackService.save(feedback);
        if (save){
            setCache();
            return CommonResult.success("success");
        }
        return CommonResult.failed();
    }

    @ApiOperation("所有反馈")
    @GetMapping("/get")
    public CommonResult get(){
       return CommonResult.success(cache.get(CACHE_NAME));
    }

    public List<Feedback> getList(){
       return feedbackService.list();
    }
}
